package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.*;
import cn.sxt.clinic.service.IDeptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-29
 */
@Controller
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("dept:deptManager")
    @RequestMapping("/deptManager")
    public String deptManager(){

        return "deptManager";
    }

    /*编辑页面跳转*/
    @RequestMapping("/deptEdit")
    public String deptManager(Long deptId, Model m){

        Dept dept = deptService.getById(deptId);
        m.addAttribute("dept",dept);

        return "deptEdit";
    }

    /*分页查询医生列表*/
    @RequiresPermissions("dept:list")
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> deptList(@RequestParam(defaultValue = "1")Integer pageNum,
                                          @RequestParam(defaultValue = "10")Integer pageSize,
                                          String keyword){
        //创建分页对象
        IPage iPage =new Page<>(pageNum,pageSize);
        //创建条件查询对象
        QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();


        IPage deptPage = deptService.page(iPage, queryWrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",deptPage.getTotal());
        map.put("rows",deptPage.getRecords());

        return map;
    }

    //删除操作
    @RequiresPermissions("dept:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public ResultCode delDept(Long deptId){

        UpdateWrapper<Dept> wrapper=new UpdateWrapper<>();
        wrapper.eq("dept_id",deptId);

        boolean remove = deptService.remove(wrapper);
        ResultCode msg=new ResultCode();
        if (remove){
            msg.setCode(1);
            msg.setMsg("删除成功");
        }else {
            msg.setCode(0);
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @RequiresPermissions("dept:insert")
    @RequestMapping("/insert")
    @ResponseBody
    public ResultCode insertDept(Dept dept){

        boolean insert = deptService.saveOrUpdate(dept);
        ResultCode msg=new ResultCode();
        if (insert){
            msg.setCode(1);
            msg.setMsg("添加科室成功");
        }else {
            msg.setCode(0);
            msg.setMsg("添加科室失败");
        }
        return msg;
    }

    @RequiresPermissions("dept:update")
    @RequestMapping("/update")
    @ResponseBody
    public ResultCode updateDept(Dept dept){

        boolean update = deptService.saveOrUpdate(dept);
        ResultCode msg=new ResultCode();
        if (update){
            msg.setCode(1);
            msg.setMsg("修改科室信息成功");
        }else {
            msg.setCode(0);
            msg.setMsg("修改科室信息失败");
        }
        return msg;
    }
}

